package com.tdtxdcxm.ruralcommunity.slice;

import com.tdtxdcxm.ruralcommunity.ResourceTable;
import com.tdtxdcxm.ruralcommunity.appruntime.DataCenterControl;
import com.tdtxdcxm.ruralcommunity.items.LCArryListContentItem;
import com.tdtxdcxm.ruralcommunity.myutils.MyTools;
import com.tdtxdcxm.ruralcommunity.providers.LCProviderForContent;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class UserContentListSlice extends AbilitySlice {
    private Button usercontent_backbut;
    private ListContainer usercontent_lc;


    /*<----------------------------------------初始化ListContainer区域----------------------------------*/
    public  void initListContainer(ListContainer listContainer,String listContainerName,AbilitySlice slice,String type){
        if(listContainer == null){
            return;
        }
        listContainer.setName(listContainerName);
        LCProviderForContent lcProviderForContent = null;

        switch (type) {
            case "dtlist":
                lcProviderForContent = new LCProviderForContent(DataCenterControl.userdt_listcontainer_list,slice);
                DataCenterControl.userDtListContainerProvider = lcProviderForContent;
                break;
            case "xmlist":
                lcProviderForContent = new LCProviderForContent(DataCenterControl.userxm_listcontainer_list,slice);
                DataCenterControl.userXmListContainerProvider = lcProviderForContent;
                break;
            case "wzlist":
                lcProviderForContent = new LCProviderForContent(DataCenterControl.userwz_listcontainer_list,slice);
                DataCenterControl.userWzListContainerProvider = lcProviderForContent;
                break;
            case "collectlist":
                lcProviderForContent = new LCProviderForContent(DataCenterControl.usercollect_listcontainer_list,slice);
                DataCenterControl.userCollectListContainerProvider = lcProviderForContent;
                break;
            default:
                break;
        }

        if(lcProviderForContent != null){
            listContainer.setItemProvider(lcProviderForContent);
        }

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Text typeText = (Text) ((DirectionalLayout)component).getComponentAt(0);
                switch(typeText.getText()){
                    case "项目":
                        slice.present(new XmDetails(),new Intent().setParam("listContainerName",listContainer.getName()).setParam("i", i));
                        break;
                    case "动态":
                    case "文章":
                        slice.present(new DtAndWzDetails(),new Intent().setParam("listContainerName",listContainer.getName()).setParam("i", i));
                        break;
                    default:
                        break;
                }
            }
        });

        listContainer.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
                LCProviderForContent lcpfc = (LCProviderForContent) listContainer.getItemProvider();
                LCArryListContentItem lcarryListContentItem = (LCArryListContentItem) lcpfc.getItem(i);
                if(!listContainer.getName().equals("usercollectlc")){
                    MyTools.UpDataDeleteUserContentInfo(UserContentListSlice.this,"delete",lcarryListContentItem.getContent_id(),lcpfc.list,lcpfc,i);
                }
                return true;
            }
        });
    }
    /*-----------------------------------------初始化ListContainer区域---------------------------------->*/


    private void initUCLSComponents(){
        usercontent_backbut = (Button) findComponentById(ResourceTable.Id_usercontent_backbut);
        usercontent_lc = (ListContainer) findComponentById(ResourceTable.Id_usercontent_lc);

        usercontent_backbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_usercontent_item_listcontainer);
        initUCLSComponents();
        String type = intent.getStringParam("type");
        switch(type){
            case "dtlist":
                initListContainer(usercontent_lc,"userdtlc",this,type);
                break;
            case "xmlist":
                initListContainer(usercontent_lc,"userxmlc",this,type);
                break;
            case "wzlist":
                initListContainer(usercontent_lc,"userwzlc",this,type);
                break;
            case "collectlist":
                initListContainer(usercontent_lc,"usercollectlc",this,type);
                break;
        }
        MyTools.DownDataUserContentListInfo(this,type);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
